package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private JedisPool jedisPool;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //新增检查套餐
        setmealDao.add(setmeal);
        //检查套餐关联检查组（操作中间表）
        this.setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.pageQuery(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Integer id) {
        setmealDao.deleteAssociationBySetmealId(id);
        String setmealImg = this.querySetmealImgById(id);
        //非空判断
        System.out.println("setmealImg = " + setmealImg);
        if(setmealImg!=null){
            QiniuUtils.deleteFileFromQiniu(setmealImg);
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmealImg);
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,setmealImg);
        }
        setmealDao.deleteSetmeal(id);
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    //编辑套餐基本信息，重新设置套餐和检查组的关联关系
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //查询原套餐img
        String setmealImgOld = this.querySetmealImgById(setmeal.getId());

        //修改redis图片信息
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmealImgOld);
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());

        //修改基本信息
        setmealDao.edit(setmeal);

        //删除套餐和检查组的关联关系
        setmealDao.deleteAssociationBySetmealId(setmeal.getId());

        //重新设置套餐和检查组的关联关系
        this.setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);

    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal findFullById(Integer id) {
        return setmealDao.findFullById(id);
    }

    public String querySetmealImgById(Integer id) {
        return setmealDao.querySetmealImgById(id);
    }


    private void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds) {
        //设置检查套餐关联检查组（操作中间表）
        if(checkgroupIds != null && checkgroupIds.length > 0){
            Map<String,Integer> map = new HashMap<>();
            for(Integer checkgroupId:checkgroupIds){
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }
}
